package mcom.xmks.bluetooth.mesh.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Locale;

import mcom.xmks.bluetooth.mesh.R;
import mcom.xmks.bluetooth.mesh.storage.NodeItem;

public  class NodeListAdapter extends RecyclerView.Adapter<NodeListAdapter.ViewHolder> {
private OnClickListener listener;
private List<NodeItem> deviceList=null;
public NodeListAdapter(OnClickListener listener){
    this.listener=listener;
}
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nodelist, parent, false);
    return new ViewHolder(item);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NodeItem nodeItem=deviceList.get(position);
        holder.name.setText(nodeItem.getName());
        holder.netAddress.setText(String.format(Locale.getDefault(),"节点地址:%04X",nodeItem.getNodeAddress()));
        holder.state.setText(nodeItem.isState()?"在线":"离线");
        holder.itemView.setOnClickListener(v -> {
            listener.onClick(nodeItem.getNodeAddress(),nodeItem.getName());
        });
        holder.itemView.setOnLongClickListener(v -> {
            listener.onLongClick(nodeItem.getNodeAddress(),nodeItem.getName());
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return deviceList!=null?deviceList.size():0;
    }
    public void loadAllNodes(List<NodeItem> items){
        deviceList=items;
        notifyDataSetChanged();
    }
    public void updateNodeState(int nodeAddress,boolean nodeState){
       if( deviceList!=null)  {
            for(int i=0; i< deviceList.size();i++){
                NodeItem nodeItem = deviceList.get(i);
                if(nodeItem.getNodeAddress()==nodeAddress){
                    nodeItem.setState(nodeState);
                    notifyItemChanged(i);
                    break;
                }
            }
        }
    }
    public void deleteNode(int nodeAddress){
        if( deviceList!=null)  {
            for(int i=0; i< deviceList.size();i++){
                NodeItem nodeItem = deviceList.get(i);
                if(nodeItem.getNodeAddress()==nodeAddress){
                    deviceList.remove(i);
                    notifyItemRemoved(i);
                    break;
                }
            }
        }
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView netAddress,name,state;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            netAddress=itemView.findViewById(R.id.netAddress);
            name=itemView.findViewById(R.id.name);
            state=itemView.findViewById(R.id.state);
        }
    }

    public interface OnClickListener{
        void onClick(int netAddress,String name);
        void onLongClick(int netAddress,String name);
    }
}
